#!/bin/sh
set -x
################################################################################
# Title          : generateDocumentationAndDeploy.sh
# Date created   : 16Nov2018
# Original Author: "Jeroen de Bruijn"
# based on https://gist.github.com/vidavidorra/548ffbcdae99d752da02
#
# Preconditions:
# - Packages doxygen graphviz must be installed.
# - An gh-pages branch should already exist. See below for mor info on how to
#   create a gh-pages branch.
#
# This script will generate Doxygen documentation and push the documentation to
# the gh-pages branch of a repository specified by $TRAVIS_REPO_SLUG
# Before this script is used there should already be a gh-pages branch in the
# repository.
#
# This file is processed by CMAKE to get the version in the commit message
# 
################################################################################

##### Setup this script and get the current gh-pages branch.
echo 'Setting up the script...'
GH_REPO_NAME=$(echo "$BUILD_REPOSITORY_NAME" | awk -F/ '{print $2}')

# Exit with nonzero exit code if anything fails
set -e

# by the time this script is run, we should have already made the docs
cd "$BUILD_SOURCESDIRECTORY/build"
#docs should be in the $BUILD_SOURCESDIRECTORY/build/html directory
if [ ! -d "html" ] || [ ! -f "./html/v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@/@PROJECT_NAME@/index.html" ]; then
	echo '' >&2
	echo 'Warning: No documentation (html) files have been found!' >&2
	echo 'Warning: Not going to push the documentation to GitHub!' >&2
	exit 0
fi

if [ -z "${BUILD_SOURCEBRANCH}" ] ; then
	echo 'Warning: Not a tag' >&2
	echo 'Warning: Not going to push the documentation to GitHub!' >&2
	exit 0
fi

if [ "${BUILD_SOURCEBRANCH}" = "refs/heads/master" ] || [ "${BUILD_SOURCEBRANCH}" = "refs/tags/v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@" ]; then
	# Fetch gh-pages branch
	git fetch origin +refs/heads/gh-pages:gh-pages
	git checkout gh-pages

	# Remove everything currently in the gh-pages branch.
	# GitHub is smart enough to know which files have changed and which files have
	# stayed the same and will only update the changed files. So the gh-pages branch
	# can be safely cleaned, and it is sure that everything pushed later is the new
	# documentation.
	rm -rf v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@/*
	cd ../
	cp -r ./build/html/v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@ ./
	cp -r ./build/html/v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@/* ./master
	rm -rf build
	rm -rf bindings

	##### Configure git.
	# Set the push default to simple i.e. push only the current branch.
	git config --global push.default simple

	git config user.name "Autogenerated by Azure Pipelines"
	git config user.email "cse-ci-notifications@analog.com"

	# Need to create a .nojekyll file to allow filenames starting with an underscore
	# to be seen on the gh-pages site. Therefore creating an empty .nojekyll file.
	# Presumably this is only needed when the SHORT_NAMES option in Doxygen is set
	# to NO, which it is by default. So creating the file just in case.
	if [ ! -f ".nojekyll" ] ; then
		touch .nojekyll
	fi

	################################################################################
	##### Upload the documentation to the gh-pages branch of the repository.   #####

	echo 'Uploading documentation to the gh-pages branch...'
	# Add everything in this directory (the Doxygen code documentation) to the
	# gh-pages branch.
	#
	# GitHub is smart enough to know which files have changed and which files have
	# stayed the same and will only update the changed files.
	git add --all

	# Commit the added files with a title and description containing the project
	# name and the GitHub commit reference that issued this build.
	git commit --allow-empty --amend -m "Deploy autogenerated docs for ${GH_REPO_NAME} v@LIBIIO_VERSION_MAJOR@.@LIBIIO_VERSION_MINOR@-g@LIBIIO_VERSION_GIT@" --sign

	# Force push to the remote gh-pages branch.
	git push --force origin gh-pages
fi
